package com.jb.web.bean.inspector.renderer;

import com.jb.web.bean.Page;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.context.WebContext;
import com.jb.web.renderer.RendererImpl;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;

public class ToolPageRenderer extends RendererImpl {


    public StringBuffer renderBegin(WebContext pCtx, 
                                    UIComponent itemToRender) {
        StringBuffer html = new StringBuffer("");
        Page page = (Page)itemToRender;
     
        if (pCtx.isRunTime()) {
            html.append("<script >\n<!--\n");
            html.append("function jbLoadPage(){\n");

            if ((pCtx.getOnLoad() != null) && pCtx.isRunTime()) {
                html.append(pCtx.getOnLoad());
            }

            html.append("\n}\n-->\n</script >\n");
        }

        html.append("<div ").append(addTagProperty("style", page.getStyle(pCtx), null))
        .append(addTagProperty("class",  page.getStyleClass(),  null))
            .append(addTagProperty("onClick", "JavaBase.EventFactory.cancelBubbleFor(event);", null))
            .append(">");

        return html;
    }

    public StringBuffer renderChildren(WebContext pCtx, 
                                       UIComponent itemToRender) {
        StringBuffer html = new StringBuffer("");
        Iterator iter = 
            ((UIComponentImpl)itemToRender).getChildrenSorted(null).iterator();

        if (!iter.hasNext()) {
            html.append("Please add component to this page");
        }

        while (iter.hasNext()) {
            UIComponentImpl aSect = (UIComponentImpl)iter.next();

            if (isVisible(pCtx, aSect)) {
                html.append(aSect.render(pCtx));
            }
        }

        return html;
    }

    public StringBuffer renderEnd(WebContext pCtx, UIComponent itemToRender) {
        Page page = (Page)itemToRender;
        StringBuffer html = new StringBuffer("</div>");

        return html;
    }
}
